package com.chanjiedata.changbs.module.main;

/**
 * 文件名 : PartnerActivity
 * 创建者 : MangoWang
 * 创建日期 : 2019/11/25 15:05
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */

import android.graphics.Color;
import android.os.Bundle;

import com.chanjiedata.changbs.R;
import com.chanjiedata.changbs.base.BaseActivity;
import com.chanjiedata.changbs.utlis.StatusBarUtil;
import com.google.android.material.tabs.TabLayout;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.widget.EditText;
import android.widget.ImageView;

import android.widget.TextView;



import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class PartnerActivity extends BaseActivity {


    Unbinder unbinder;
    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    private PartnerTypeFragment fragment_one;
    private PartnerTypeFragment fragment_two;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_partner);
        unbinder = ButterKnife.bind(this);
        StatusBarUtil.setStatusBarLightMode(this, Color.parseColor("#ffffff"), true);
        backIv.setOnClickListener(view -> finish());
        initView();
        initListener();
    }

    private void initListener() {
        tvSearch.setOnClickListener(view -> {
            int selectedTabPosition = tabLayout.getSelectedTabPosition();
            String searchKey = etSearch.getText().toString().trim();
            if (selectedTabPosition == 0) {
                fragment_one.searchData(searchKey);
            } else if (selectedTabPosition == 1) {
                fragment_two.searchData(searchKey);
            }
        });
    }

    protected void initView() {
        String[] titles = {"商户(0)", "渠道(0)"};
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(fragment_one = PartnerTypeFragment.NewInstance(0));
        fragmentList.add(fragment_two = PartnerTypeFragment.NewInstance(1));
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return titles[position];
            }
        });
        //设置图标
        tabLayout.setupWithViewPager(viewPager);
        int page = getIntent().getIntExtra("page", 0);
        if (page != 0) {
            viewPager.setCurrentItem(page);
        }
    }





    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }


    public void changeTabTitle(int position, String total) {
        if (tabLayout != null) {
            TabLayout.Tab tabAt = tabLayout.getTabAt(position);
            if (tabAt != null) {
                String title = "";
                if (position == 0) {
                    title = "商户(" + total + ")";
                } else if (position == 1) {
                    title = "渠道(" + total + ")";
                }
                tabAt.setText(title);
            }
        }
    }
}
